/*
 * Filename CheckItemCategoryRepositoryImpl.java 2022年03月24日
 * Copyright © Ehome Co.Ltd. All Rgiths Reserved.
 * @author wuyingchao
 */
package com.ehome.poct.core.dao.mybatis;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ehome.poct.core.api.dto.check.CheckItemCategoryDTO;
import com.ehome.poct.core.dao.entity.check.CheckItemCategoryDO;
import com.ehome.poct.core.dao.entity.check.CheckProjectItemDO;
import com.ehome.poct.core.dao.mapping.CheckCategoryMapping;
import com.ehome.poct.core.dao.mybatis.mapper.CheckItemCategoryMapper;
import com.ehome.poct.core.dao.mybatis.mapper.CheckProjectItemMapper;
import com.ehome.poct.core.repository.check.CheckItemCategoryRepository;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wyc
 * @since 2022/03/24 10:49
 **/
@Repository
@AllArgsConstructor
public class CheckItemCategoryRepositoryImpl implements CheckItemCategoryRepository {
    private final CheckItemCategoryMapper categoryMapper;
    private final CheckProjectItemMapper itemMapper;
    private final CheckCategoryMapping categoryMapping;

    @Override
    public List<CheckItemCategoryDTO> findByProjectCode(Integer projectCode) {
        final var categoryDOS = categoryMapper.selectList(Wrappers.<CheckItemCategoryDO>lambdaQuery()
                .eq(CheckItemCategoryDO::getProjectCode, projectCode));
        final var categoryDTOS = categoryMapping.toCategory(categoryDOS);

        return categoryDTOS.stream().peek

                (c -> {
            final var ids = Arrays.stream(c.getCheckItemIds().split(",")).map(i -> Long.valueOf(i)).collect(Collectors.toList());
            final var itemDOs = itemMapper.selectList(Wrappers.<CheckProjectItemDO>lambdaQuery().in(CheckProjectItemDO::getBloodCheckProjectItemId, ids));
            c.setItems(categoryMapping.toProjectItem(itemDOs));
        }).collect(Collectors.toList());
    }
}